<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html:html>
<head>	
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/sampleForm.css" />
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/me.css" />	
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/common/css/help/help.css" />
	<title>บันทึกใบซื้ออุปกรณ์, วัตถุดิบ (BCAPInvoice, BCAPInvoiceSub)edit</title>
	<script type='text/javascript' src='/ittl/dwr/interface/BCProjectService.js'></script>
  	<script type='text/javascript' src='/ittl/dwr/interface/BCItemService.js'></script>
	
	<!-- 
  	<script type='text/javascript' src='/ittl/dwr/interface/ItemLocationService.js'></script>
  	 -->
  	
  	
  	<script type='text/javascript' src='/ittl/dwr/interface/BCAPInvoiceService.js'></script>
  	<script type='text/javascript' src='/ittl/dwr/engine.js'></script>
  	<script type='text/javascript' src='/ittl/dwr/util.js'></script>

	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.bgiframe.min.js1' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.ajaxQueue.js1' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/thickbox-compressed.js1' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.autocomplete.js' ></script>
	
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/localdata.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/localdata2.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/codeOfBuyRequest.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/localdata31.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/localdataDepartment.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/localdataDepartmentName.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/apCodeName.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/data/apCode.js' ></script>
	
	
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/autocomplete/css/mainOLD.css" />
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/autocomplete/css/jquery.autocomplete.css" />
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/autocomplete/css/thickbox.css" />
 
 	<script type="text/javascript" src="<%= request.getContextPath() %>/stockRequest/jquery.validate.js" ></script>
 	<script type="text/javascript" src="<%= request.getContextPath() %>/stockRequest/jquery-1.4.2.min1.js" ></script>

<!-- start 
 
	<link type="text/css" href="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" />
	<link type="text/css" href="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/demos/demos.css" rel="stylesheet" />
	  	
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/jquery-1.4.2.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/external/jquery.bgiframe-2.1.1.js"></script>			
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.core.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.widget.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.mouse.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.button.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.draggable.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.position.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.resizable.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.dialog.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.effects.core.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.datepicker.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.autocomplete.js"></script>
			
-->
<!-- end -->

<!-- start add/remove  
	<script type="text/javascript" src='<%= request.getContextPath() %>/common/js/addremove/jquery-latest.pack.js'></script>	
-->	
	<script type="text/javascript" src='<%= request.getContextPath() %>/common/js/addremove/jquery.highlightFade.js'></script>
	<script type="text/javascript">
		
	var id = 1;	

	var idArray = new Array();
	for(i=0;i<=15;i++) {
		idArray[i] = 0;	
	}

	var max = false;
	
	function addFormField() {
		max = true;
		for (i=1; i<idArray.length; i++) {
			if(idArray[i]==0) {
				
				
				
				max = false;
				idArray[i] = 1;
				id = i+1; 
				i = idArray.length;
								
				var newInput = "<tr id='r"+id+"'>"+
									"<td><a href='#' onClick='removeFormField(\"#r" + id + "\","+id+"); return false;'>"+
										"<img src=\"<%=request.getContextPath() %>/common/images/remove.png\" width=\"24\" height=\"24\" alt=\"Remove\" /></a></td>"+
									"<td><input	type='text' name='itemCode"+id+"' value='' size='12' "+
												" onblur=\"getData(document.getElementById('itemCode"+id+"').value,"+id+")\" id=\"itemCode"+id+"\"> "+ 											     
										"</td><td></td> "+
										"<td><input type='text' name='itemName"+id+"' value='' maxlength='200' size='40' id='itemName"+id+"' >"+														
									"</td><td></td>"+

									
									"<td align='center'><input type='text' name='unitName"+id+"' value='' size='5' id='unitName"+id+"' >"+																																
									"	 <input	type='hidden' name='unitCode"+id+"' value='' id='unitCode"+id+"'>"+
									"</td><td></td>"+									
									"<td><input	type='text'	name='qty"+id+"' value=''  size='5' id='qty"+id+"' onblur='calLineItem("+id+");' ></td><td></td>"+
									"<td><input	type='text'	name='price"+id+"' value='' size='5' id='price"+id+"' onblur='calLineItem("+id+");' ></td>"+
									"<td></td>"+
									"<td><input	type='text'	name='amount"+id+"' value='' size='5' id='amount"+id+" ></td>"+
									"<td></td>"+									
									"<td><input	type='text'	name='division"+id+"' value='' id='division"+id+"' ></td>"+
									"<td></td>"+
								"</tr>";
																						  
																					
//							<td>${fullForm.stkReqNo1 }<input	type="hidden" name="stkReqNo1" value="${fullForm.stkReqNo1}" id="stkReqNo1"></td>
//							<td><input	type="hidden" name="confirmNo1" value="${fullForm.confirmNo1}" id="confirmNo1"></td>

								
				
				
				$("#inputs").append(newInput); 
				$('#r' + id).highlightFade({
					speed:2000
				});			
				$("#itemCode1a"+id).autocomplete(codes, {
					max:30,
					minChars: 0,
					autoFill: true,
					mustMatch: true,
					matchContains: false,
					scrollHeight: 200				
				});

				$("#itemCode"+id).autocomplete(itemsName, {
					minChars: 0,
					width: 400,
					max:50,
					mustMatch: true,
					matchContains: true,
					autoFill: false,
					formatItem: function(row, i, max) {
						return row.code + ": [" + row.name + "]";
					},
					formatMatch: function(row, i, max) {
						return row.code + ": [" + row.name + "]";
					},
					formatResult: function(row) {
						return row.code;
					}
				});								
			}
		}
						
		if(max==true) {											 	
			alert("คุณสามารถเพิ่มข้อมูลได้สูงสุด 15 รายการ");
		}				
	}
	
	function removeFormField(rid, id) {
		var count = 0;
		for (i=0; i<idArray.length; i++) {
			if(idArray[i]>0) {
				count++;
			}
		}		
		if(count>1) {
			$(rid).remove();
			idArray[id-1] = 0;
		}else {
			alert("กรุณาบันทึกข้อมูลอย่างน้อย 1 รายการ");
		}
		
	}
		
	</script>
<!-- end add/remove  -->

<style type="text/css">
#form1 label.error {
	font-size: 1.0em;
	color: red;
}
</style>
 	
<script language="JavaScript">
 
function compareBuga() {
	return "buga22";
}

$.validator.addMethod("buga", function(value) {
	return value == compareBuga();
}, 'Please enter "buga"!');

$(document).ready(function() {
	 
	
	$("#form1").validate({
		
			
		submitHandler: function() {
			alert("txt1="+document.getElementById("txt1").value);
			alert("submitted!22");
			//$('#dialog-confirm').dialog('open');
			form.submit();
		},
				
		rules: {
			txt1: "required",
			docNo: "required",
			docDate: "required",
			apCode: "required",
			iTTL_deptPay: "required",
			projectCode: "required"
			
		},
		messages: {
			txt1: "*",
			docNo: "*",
			docDate: "*",
			apCode: "*",
			iTTL_deptPay: "*",
			projectCode: "*"
			
		},

		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			//alert("errorPlacement");			
			if ( element.is(":radio") ) {
				alert("radio");
				error.appendTo( element.parent().next().next() );
			}else if ( element.is(":checkbox") ) {
				alert("checkbox");
				error.appendTo ( element.next() );
			}else {
				/*alert(element("#itemCode1"));
				if(element.is("#itemCode1")) {
					error.appendTo( "#qty1-error" );
				}else {
					error.appendTo( element.parent().next() );	
				}*/
				//error.insertAfter('#qty1-error');
				error.appendTo( element.parent().next() );
			}
		}		
	});
	

	$("#suggest1").focus().autocomplete(cities);
	
	$("#suggestProject").autocomplete(projects, {		
		max:30,
		minChars: 0,
		autoFill: true,
		mustMatch: true,
		matchContains: false,
		scrollHeight: 200
	});

	
	$("#departCode").autocomplete(departments, {		
		max:30,
		minChars: 0,
		autoFill: true,
		mustMatch: true,
		matchContains: false,
		scrollHeight: 200
	});
			
	$("#suggestDeptPay").autocomplete(departments, {		
		max:30,
		minChars: 0,
		autoFill: true,
		mustMatch: true,
		matchContains: false,
		scrollHeight: 200
	});
			
	$("#suggestCode1").autocomplete(codes, {
		max:30,
		minChars: 0,
		autoFill: true,
		mustMatch: true,
		matchContains: false,
		scrollHeight: 200				
	});
	

	$("#suggest4").autocomplete(emails, {
		minChars: 0,
		
		matchContains: true,
		autoFill: false,
		formatItem: function(row, i, max) {
			return i + "/" + max + ": \"" + row.name + "\" [" + row.to + "]";
		},
		formatMatch: function(row, i, max) {
			return row.name + " " + row.to;
		},
		formatResult: function(row) {
			return row.to;
		}
	});

	for(i=1;i<=15;i++) {
		$("#itemCode"+i+"old").autocomplete(itemsName, {
			minChars: 0,
			width: 400,
			max:50,
			mustMatch: true,
			matchContains: true,
			autoFill: false,
			formatItem: function(row, i, max) {
				return row.code + ": [" + row.name + "]";
			},
			formatMatch: function(row, i, max) {
				return row.code + ": [" + row.name + "]";
			},
			formatResult: function(row) {
				return row.code;
			}
		});
	}

				
});

</script>  

<!--  ส่วนที่ 2 -->
<script language="JavaScript">
var inputNo;
var itemName = "";
var code;
var itemName;
var whCode;
var shelfCode;
var shelfName;

function getItem(var1,x) {	
	inputNo = x;
	BCItemService.getItem( var1, loadinfo2 );	  
}

<!--  ส่วนที่ 3 -->
function loadinfo2(item)
{
	code 	  = item.code;
	if(code=="") {
		alert("ไม่พบ "+document.getElementById('itemCode'+inputNo).value + "  ในระบบ");
		//dwr.util.setValue("itemCode"+inputNo, "");
		document.getElementById('itemCode'+inputNo).focus();
	}else {
		itemName  = item.itemName;
		
		whCode 	  = "10";//item.whCode;
		shelfCode = "10";//item.shelfCode;
		shelfName = item.shelfName;
		unitCode = item.unitCode;
		unitName = item.unitName;
		ittl_row = item.ittl_row;
		ittl_shelf = item.ittl_shelf;
		
		dwr.util.setValue("itemCode"+inputNo, code);
		dwr.util.setValue("itemName"+inputNo, itemName);
		dwr.util.setValue("whCode"+inputNo, whCode);			
		dwr.util.setValue("unitCode"+inputNo, unitCode);
		dwr.util.setValue("unitName"+inputNo, unitName);			
		dwr.util.setValue("ittl_row"+inputNo, ittl_row);
		dwr.util.setValue("ittl_shelf"+inputNo, ittl_shelf);
	}											
}

function getData(var1,x) {	
	inputNo = x;
	ItemLocationService.getData( var1, loadinfo );	  
}

//<!--  ส่วนที่ 3 -->
function loadinfo(data)
{
	code 	  = data.code;
	itemName  = data.name1;
	whCode 	  = data.whCode;
	shelfCode = data.shelfCode;
	shelfName = data.shelfName;
	unitCode = data.unitCode;
	unitName = data.unitName;

	ittl_row = data.ittl_row;
	ittl_shelf = data.ittl_shelf;
	
	
		dwr.util.setValue("itemCode"+inputNo, code);
			dwr.util.setValue("itemName"+inputNo, itemName);
			dwr.util.setValue("whCode"+inputNo, whCode);			
			dwr.util.setValue("unitCode"+inputNo, unitCode);
			dwr.util.setValue("unitName"+inputNo, unitName);
			//redirectN(0, document.getElementById('shCode1'));
			//redirectN(0, document.getElementById('shCode5'));//ใช้ document.form1.shelfCode5 จะทำงานได้บน IE แต่ไม่ไได้บน FF
			dwr.util.setValue("ittl_row"+inputNo, ittl_row);
			dwr.util.setValue("ittl_shelf"+inputNo, ittl_shelf);
								
			
}

function redirectN(x, tempN){
	
	var groupsN=5;//document.form1.shelfCode1.options.length
	var groupN=new Array(groupsN);
	for (i=0; i<groupsN; i++)
		groupN[i]=new Array();

	//var tempN=document.form1.shelfCode1;	
	//alert("tempN.options.length-1 = "+(tempN.options.length-1));
	for (m=tempN.options.length-1;m>=0;m--) {
		//alert(tempN.options[m]);
		tempN.options[m]=null;
	}
	
	if(shelfName != null) {
		for (i=0; i<shelfName.length; i++) {
			groupN[0][i]=new Option(shelfName[i], shelfCode[i]);
		}
					
		for (i=0;i<groupN[x].length;i++){
			text = groupN[x][i].value+":"+groupN[x][i].text;
			tempN.options[i]=new Option(text,groupN[x][i].value);
		}
		tempN.options[0].selected=true;
	}
}
</script>
<!--  ส่วนที่ 2 -->
<script language="JavaScript">

function getProject() {
	//alert("bcproject"+form1.projectCode.value); ใช้ใน ff ไม่ผ่าน			
	BCProjectService.getProject( document.getElementById("suggestProject").value, loadinfoProject );			  
}

//<!--  ส่วนที่ 3 -->
function loadinfoProject(project){		
	//dwr.util.setValue("projectCode", project.name);
	dwr.util.setValue("projectName", project.name);			     
}
</script>

<!--  ส่วนที่ 2 -->
<script language="JavaScript">

function getMaxDocNo() {
	
	var sel = document.getElementById("preDocNo");			
	BCAPInvoiceService.getMaxDocNo(sel.options[sel.selectedIndex].value, loadinfoDocNo);			  
}

//<!--  ส่วนที่ 3 -->
function loadinfoDocNo(maxDocNo){		
	
	dwr.util.setValue("docNo", maxDocNo.docNo);

//	alert("aa");
	var mySelect = document.getElementById("preDocNo");
	
	for(var i=0; i<mySelect.options.length; i++) {
		if(mySelect.options[i].selected==true && mySelect.options[i].value=="BCS-") {
			document.getElementById("refNo").options[1].selected=true;		
			break;
		}else {
			document.getElementById("refNo").options[0].selected=true;
		}
	}
				     
}
</script>	

<script type="text/javascript">
function alertValue() {
	alert(document.getElementById('htmlElementTest').value)
}
</script>

	<link type="text/css" href="<%= request.getContextPath() %>/css/datepicker/css/ui.all.css" rel="stylesheet" /> 
	
	<script type="text/javascript" src="<%= request.getContextPath() %>/css/datepicker/lib/jquery-ui-1.5.3.min.js"></script>
	

	<script type="text/javascript">
		$(document).ready(function(){
    			$("#docDate").datepicker({ altField: 'input#docDate', altFormat: 'yy-mm-dd' });
    			$("#taxDate").datepicker({ altField: 'input#taxDate', altFormat: 'yy-mm-dd' });
  		});
	</script>

<script type="text/javascript">
	var sumOfLineItemAmount = new Array();
	var sumOfAmount = 0;
	var qty = 0;
	var price = 0;
	var disAmt =0;
	var discountAmount = 0;
	var taxAmount = 0;
	
	var qtyArray = new Array();
	for(var i=1; i<=50; i++) {
		qtyArray[i]=0;
		sumOfLineItemAmount[i] = 0;
	}
	
function calUnitPrice(id) {
	
	qty = document.getElementById('qty'+id).value;	
	disAmt = document.getElementById('lineItemDiscountAmount'+id).value
	if(disAmt=="") {
		disAmt = 0;
	}	

	sumOfAmount -= sumOfLineItemAmount[id];

	sumOfLineItemAmount[id] = document.getElementById('amount'+id).value;
	sumOfAmount = sumOfAmount*1+sumOfLineItemAmount[id]*1;

	document.getElementById('lineItemDiscountAmount'+id).value =0;
	document.getElementById('price'+id).value = sumOfLineItemAmount[id]/qty;

	var discountAmount = document.getElementById('discountAmount').value;
	
	document.getElementById('sumOfItemAmount').value = sumOfAmount;

	var vat = 0;
	if(document.getElementById('taxNo').value!="") {
		vat = calTaxAmount();
		alert(vat);
		document.getElementById('taxAmount').value = parseFloat(vat).toFixed(2);
	}else {
		document.getElementById('taxAmount').value = parseFloat("0").toFixed(2);;
	}
	
	document.getElementById('totalAmount').value = parseFloat(sumOfAmount-discountAmount).toFixed(2);
	document.getElementById('netAmount').value = parseFloat(sumOfAmount-discountAmount).toFixed(2);
	document.getElementById('payAmount').value = parseFloat(sumOfAmount-discountAmount).toFixed(2);
	//document.getElementById('lineItemDiscountAmount'+id).value=disAmt;			 
}

function calLineItem(id) {
	
	qty = document.getElementById('qty'+id).value;	
	price = document.getElementById('price'+id).value;
	disAmt = document.getElementById('lineItemDiscountAmount'+id).value;
	if(disAmt=="") {
		disAmt = 0;
	}
	sumOfLineItemAmount[id] = parseFloat(qty*price-disAmt).toFixed(2);
	
	document.getElementById('amount'+id).value = parseFloat(sumOfLineItemAmount[id]).toFixed(2);
	document.getElementById('lineItem_netAmount'+id).value = parseFloat(sumOfLineItemAmount[id]).toFixed(2);
	document.getElementById('lineItem_homeAmount'+id).value = parseFloat(sumOfLineItemAmount[id]).toFixed(2);
	
	sumOfAmount = calSumAmount();//sumOfAmount*1+sumOfLineItemAmount[id]*1;
	
	document.getElementById('discountAmount').value = parseFloat("0").toFixed(2);	
	document.getElementById('sumOfItemAmount').value = parseFloat(sumOfAmount).toFixed(2);
	
	var vat = 0;
	if(document.getElementById('taxNo').value!="") {
		vat = calTaxAmount();
		document.getElementById('taxAmount').value = parseFloat(vat).toFixed(2);
	}else {
		document.getElementById('taxAmount').value = parseFloat("0").toFixed(2);;
	}
	document.getElementById('totalAmount').value = parseFloat(sumOfAmount*1+vat*1).toFixed(2);
	document.getElementById('netAmount').value = parseFloat(sumOfAmount*1+vat*1).toFixed(2);
	document.getElementById('payAmount').value = parseFloat(sumOfAmount*1+vat*1).toFixed(2);
	document.getElementById('price'+id).value = parseFloat(price).toFixed(2);
	document.getElementById('lineItemDiscountAmount'+id).value=parseFloat(disAmt).toFixed(2);	
}

function calTaxAmount() {
	sumOfItemAmount = 0;
	discountAmount = 0;
	if(document.getElementById('taxNo').value!="") {
		sumOfItemAmount = parseFloat(document.getElementById('sumOfItemAmount').value);

		if(document.getElementById('discountAmount').value!="") {
			discountAmount = parseFloat(document.getElementById('discountAmount').value);										
		}	
		var taxAmount = parseFloat((sumOfItemAmount-discountAmount)*0.07).toFixed(2);
		
		document.getElementById('taxAmount').value  = parseFloat(taxAmount).toFixed(2);
		document.getElementById('totalAmount').value= parseFloat(sumOfItemAmount*1+taxAmount*1-discountAmount).toFixed(2);
		document.getElementById('netAmount').value 	= parseFloat(sumOfItemAmount*1+taxAmount*1-discountAmount).toFixed(2);
		document.getElementById('payAmount').value 	= parseFloat(sumOfItemAmount*1+taxAmount*1-discountAmount).toFixed(2);
									
	}else {
		if(document.getElementById('sumOfItemAmount').value!="") {
			sumOfItemAmount = parseFloat(document.getElementById('sumOfItemAmount').value);
		}
		if(document.getElementById('discountAmount').value!="") {
			discountAmount = parseFloat(document.getElementById('discountAmount').value);										
		}		
		document.getElementById('taxAmount').value  = parseFloat("0").toFixed(2);		
		document.getElementById('totalAmount').value= parseFloat(sumOfItemAmount*1-discountAmount).toFixed(2);
		document.getElementById('netAmount').value 	= parseFloat(sumOfItemAmount*1-discountAmount).toFixed(2);
		document.getElementById('payAmount').value 	= parseFloat(sumOfItemAmount*1-discountAmount).toFixed(2);
	}
	
	document.getElementById('discountAmount').value = parseFloat(discountAmount).toFixed(2);
	document.getElementById('sumOfItemAmount').value = parseFloat(sumOfItemAmount).toFixed(2);
	
	return taxAmount;
}

function dataToPayAmountOLD() {
	//alert("datatopay");
	var mySelect = document.getElementById("preDocNo");
	for(var i=0; i<mySelect.options.length; i++) {
		if(mySelect.options[i].selected==true && mySelect.options[i].value=="BCS-") {
			document.getElementById('payAmount').value = document.getElementById('netAmount').value;					
			break;
		}else {
			document.getElementById('payAmount').value = 0;
		}
	}	
}

function calSumAmount() {
	sumOfAmount = 0;
	for(var i=1; i<=50; i++) {						
		if(document.getElementById('amount'+i)!=null) {			
			sumOfLineItemAmount[i]=document.getElementById('amount'+i).value;		
			sumOfAmount = sumOfAmount*1+sumOfLineItemAmount[i]*1;
		}
		//sumOfAmount = sumOfAmount*1+sumOfLineItemAmount[i]*1;
	}
//	alert(sumOfAmount);
	return sumOfAmount;
}

function checkKey() {
	alert(envent.keyCode);
}
</script>
<script type="text/javascript" >
function modalWin(aa) {
	if (window.showModalDialog) {		
		window.showModalDialog(aa,"name","dialogTop:100px;dialogWidth:650px;dialogHeight:350px;center:yes");
	} else {		
		window.open(aa,'name','height=350,width=600,toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=no,resizable=no ,modal=yes');
	}
}
function modalWinItem(aa) {
	if (window.showModalDialog) {		
		window.showModalDialog(aa,"name","dialogTop:100px;dialogWidth:650px;dialogHeight:350px;center:yes");
	} else {		
		window.open(aa,'name','height=350,width=600,toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=no,resizable=no ,modal=yes');
	}
}
function modalWinDocNo(aa) {
	if (window.showModalDialog) {		
		window.showModalDialog(aa,"name","dialogTop:100px;dialogWidth:250px;dialogHeight:200px;center:yes");
	} else {		
		window.open(aa,'name','height=350,width=600,toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=no,resizable=no ,modal=yes');
	}
}

</script>
</head>

<body><br />
<div id="wrapper">
	 
	<div id="menu"><ul id="nav"></ul></div>

	<div id="content">
		<div id="menuNav"><a href='<%=request.getContextPath() %>/Login.do?mode=check'>หน้าหลัก</a> 
		>> คลังพัสดุ - Store กลาง  >> จัดซื้อ
		>> <html:link action="/apinvoice2.do?mode=showFind" >ดูรายการซื้อสินค้าและบริการ(เงินสด)</html:link> 
		>> บันทึกซื้อสินค้า และบริการ </div><br />
			
	<div style="background: #ffffff;">
	<p align="center" id="titleIssueType">บันทึกใบซื้ออุปกรณ์, วัตถุดิบ </p>
	
	<html:form action="/apinvoiceUpdate.do?mode=update&param=Update" styleId="form1"  >	        
                
                <html:hidden property="creatorCode" value="${creatorCode }" />
                <html:hidden property="lastEditorCode" value="${userName }" />
                                
	<div id="userPw01">
	<fieldset>
		<legend><b>ข้อมูลใบซื้ออุปกรณ์, วัตถุดิบ</b></legend>
		   <center> 
		   <table border="0" >
			<tr>
				<td width="160" align="right">ประเภทการซื้อ:</td>
				<td width="140"><html:select property="billType" value="${billType}" style="width:115px">
									<html:option value="0">ซื้อสินค้าเงินสด</html:option>
									<html:option value="1">ซื้อสินค้าเงินเชื่อ</html:option>
									<html:option value="2">ซื้อบริการเงินสด</html:option>
									<html:option value="3">ซื้อบริการเงินเชื่อ</html:option>
								</html:select>		
								</td>
				<td width="20"></td>				
				<td width="160" align="right">ประเภทการซื้อ:</td>
				<td width="140">
								<html:select property="taxType" value="${taxType}" style="width:115px">
									<html:option value="0">ภาษีแยกนอก</html:option>
									<html:option value="1">ภาษีรวมใน</html:option>
									<html:option value="2">ภาษีอัตราศูนย์</html:option>
								</html:select></td>
				<td width="20"></td>
			</tr>		   		   
			<tr>
				<td width="160"align="right">เลขที่ใบซื้อ:</td>
				<td width="140">
					<html:text property="docNo" value="${docNo}" styleId="docNo" readonly="true" 
								errorKey="org.apache.struts.action.ERROR" style="width:115px" />
				</td>
					<html:errors property="docNo" />
				<td width="20"></td>				
				<td width="160"align="right"> วันที่ออกเอกสาร:</td>
				<td width="160"><html:text property="docDate" styleId="docDate" value="${docDate}" style="width:115px" 
											errorKey="org.apache.struts.action.ERROR" readonly="false" />
								<html:errors property="docDate" />				
				</td>
				<td width="20"></td>					
			</tr>
			<tr>
				<td width="160"align="right"> เลขที่ใบกำกับภาษี:</td>
				<td width="140">
					<html:text property="taxNo" styleId="taxNo" value="${taxNo}"  onchange="calTaxAmount()" style="width:115px" 
								errorKey="org.apache.struts.action.ERROR" />
					<html:errors property="taxNo" />					
				</td>
				<td width="20"></td>				
				<td width="160"align="right"> วันที่ใบกำกับภาษี:</td>
				<td width="160">
					<html:text property="taxDate" styleId="taxDate" value="${taxDate}"style="width:115px" 
								errorKey="org.apache.struts.action.ERROR" readonly="false" />
					<html:errors property="taxDate" />
				</td>
				<td width="20"></td>					
			</tr>
			<tr>			
 				<td align="right">รหัสเจ้าหนี้:</td>
 				<td> 
 					<html:text property="apCode" value="${apCode}" styleId="apCode" readonly="true" style="width:115px" />										 					
 					<html:errors property="apCode" />
 					 					
 				</td>
 				<td ><a href="#" target="name" onclick="modalWinItem('/ittl/apinvoice/ap.do?mode=apFind'); return false;">ค้น</a></td> 				
 				<td colspan="3">
 					<html:text property="apName" value="${apName}" size="40" styleId="apName" style="width:277px" readonly="true"/>
 				</td> 										 				 			
 			</tr>													 			 				 							
		</table>
		</center>
     </fieldset></div>
<div id="items"> 
     <fieldset>
     	<legend><b>รายการอุปกรณ์, วัตถุดิบ</b></legend>     	
<!--      	     	
     	<p><a href="#" onClick="addFormField(); return false;">
     			<img src="<%=request.getContextPath() %>/common/images/add.png" width="24" height="24" alt="Add" />
     		</a>     			   
     	</p>
 -->     	
     		<input type="hidden" id="id" value="0">
     	<center>	     		          	
		<table border="0" >
			<thead>
				<tr id="rHead">
					<th width="24"></th>
				  <th width="100" align="center">รหัสสินค้า</th>
				  <th width="10">&nbsp;</th>
				  <th width="270" align="center">ชื่อรายการ</th>
				  <th width="10">&nbsp;</th>				

				  <th width="70"  align="center">หน่วยนับ</th>
				  
				  <th width="60"  align="center">จำนวน</th>
				  <th width="10">&nbsp;</th>
				  <th width="60"  align="center">ราคาซื้อ</th>
				  <th width="10">&nbsp;</th>				
				  <th width="60"  align="center">ส่วนลด</th>
				  <th width="10">&nbsp;</th>				    
				  <th width="70"  align="center">จำนวนเงิน</th>
				  <th width="10">&nbsp;</th>				  
				  <th width="10"></th>
				  			
				</tr>						
			</thead>
			
			<tbody id="inputs" >								 								
				<logic:notEmpty name="resultList1" >					
					<logic:iterate id="aa" name="resultList1" indexId="id" >
						<tr id="r${id+1 }">
							<td>&nbsp;
							</td>								
						  	<td><html:hidden property="lineNumber" value="${aa.lineNumber}" styleId="lineNumber${id+1}" />
						  		<html:text property="itemCode" value="${aa.itemCode}" styleId="itemCode${id+1}" size="15"
						  				onchange="getItem(document.getElementById('itemCode${id+1}').value,${id+1})" 
						  				style="align:right;width:100px"
						  				 />
						  		<html:errors property="itemCode${id+1}" />		 						  							  		
						  	</td>
						  	<td><a href="#" target="name" onclick="modalWinItem('/ittl/apinvoice/apinvoice2.do?mode=moreItemsShow&lineNumber=${id+1 }'); return false;">ค้น</a></td>
							<td><html:text property="itemName" value="${aa.itemName}" maxlength="200" size="40" styleId="itemName${id+1}" />
								<html:errors property="itemName${id+1}" />																			
							</td><td></td>
												
							<td align="center"><html:select property="unitCode" value="${aa.unitCode}" styleId="unitCode${id+1}" style="width:60px">
													<html:option value="PCS">Pcs.</html:option>
													<html:option value="007">Kgs.</html:option>
													<html:option value="008">Lbs.</html:option>
													<html:optionsCollection name="unitCodeList" label="name" value="code" />
											   </html:select>						
							</td>
							<td><html:text property="qty" value="${aa.qty}" size="7" styleId="qty${id+1}" onchange="calLineItem(${id+1});" style="text-align:right;width:60px"  />
								<html:errors property="qty${id+1}" />
							</td>
							<td></td>
							<td><html:text property="price" value="${aa.price}" size="7" styleId="price${id+1}" onchange="calLineItem(${id+1});" style="text-align:right;width:60px" />
								<html:errors property="price${id+1}" />
								</td>
							<td></td>
							<td>
								<html:text property="lineItemDiscountAmount" value="${aa.lineItemDiscountAmount}" size="7" styleId="lineItemDiscountAmount${id+1}" onchange="calLineItem(${id+1})"	style="text-align:right;width:60px"  />
								<html:errors property="lineItemDiscountAmount${id+1}" />
							</td>
							<td></td>					
							<td><html:text property="amount" value="${aa.amount}" styleId="amount${id+1}" size="10" onchange="calUnitPrice(${id+1})" style="text-align:right;width:60px" />
								<html:errors property="amount${id+1}" />							
							</td>							
							<td>
								<html:hidden property="stkReqNo"  	value="${aa.stkReqNo}" styleId="stkReqNo${id+1}" />								
								<html:hidden property="confirmNo" 	value="${aa.confirmNo}" styleId="confirmNo${id+1}" />
								
								<html:hidden property="lineItem_whCode" 		value="${aa.lineItem_whCode }" 		 styleId="lineItem_whCode${id+1}" />
								<html:hidden property="lineItem_shelfCode" 		value="${aa.lineItem_shelfCode }" 	 styleId="lineItem_shelfCode${id+1}" />
								
								<html:hidden property="lineItem_statusReceive" 	value="${aa.lineItem_statusReceive }" styleId="lineItem_statusReceive${id+1}" />
								<html:hidden property="lineItem_departCode" 	value="${aa.lineItem_departCode }" 	  styleId="lineItem_departCode${id+1}" />
								<html:hidden property="lineItem_projectCode" 	value="${aa.lineItem_projectCode }"   styleId="lineItem_projectCode${id+1}" />
								<html:hidden property="lineItem_allocateCode" 	value="${aa.lineItem_allocateCode }"  styleId="lineItem_allocateCode${id+1}" />
								<html:hidden property="lineItem_myDescription" 	value="${aa.lineItem_myDescription }" styleId="lineItem_myDescription${id+1}" />
								<html:hidden property="lineItem_poRefNo" 		value="${aa.lineItem_poRefNo }" 	  styleId="lineItem_poRefNo${id+1}" />
								<html:hidden property="lineItem_netAmount" 		value="${aa.lineItem_netAmount }" 	  styleId="lineItem_netAmount${id+1}" />
								<html:hidden property="lineItem_homeAmount" 	value="${aa.lineItem_homeAmount }" 	  styleId="lineItem_homeAmount${id+1}" />								
							</td>								
							
							<td><label id="lineItem_moreDetails${id+1 }">
									<a href="#" target="name" onclick="modalWin('/ittl/apinvoice/apinvoice2.do?mode=moreDetailsShow&lineItem_whCode=${aa.lineItem_whCode }&lineItem_shelfCode=${aa.lineItem_shelfCode }&lineItem_statusReceive=${aa.lineItem_statusReceive }&lineItem_departCode=${aa.lineItem_departCode }&lineItem_projectCode=${aa.lineItem_projectCode }&lineItem_allocateCode=${aa.lineItem_allocateCode }&lineItem_myDescription=${aa.lineItem_myDescription }&lineItem_poRefNo=${aa.lineItem_poRefNo }&lineItem_netAmount=${aa.lineItem_netAmount }&lineItem_homeAmount=${aa.lineItem_homeAmount }&lineNumber=${id+1 }'); return false;">อื่นๆ</a>
								</label>  </td>
												
						</tr>												
					</logic:iterate>									
				</logic:notEmpty>																				
			</tbody>					
		</table>
		</center>
     </fieldset>
 	</div>    
 	<div id="userPw01">
	<fieldset>
		<legend><b>ข้อมูลใบซื้ออุปกรณ์, วัตถุดิบ</b></legend>
		   <center> 
		   <table border="0" >
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>			
 				<td align="right">รหัสผู้ติดต่อ:</td>
 				<td><html:text property="contactCode" value="${contactCode}" styleId="contactCode" readonly="true" style="width:90px" /> 				
 				</td>
 				<td ></td>				 				 				
 				<td align="right">ยอดรวมมูลค่าสินค้า:</td>
 				<td><html:text property="sumOfItemAmount" value="${sumOfItemAmount}" styleId="sumOfItemAmount" onchange="calTaxAmount();" style="text-align:right;width:90px" />
 					<html:errors property="sumOfItemAmount" />
 					</td>
 				<td ></td>	
 			</tr>		   													
			<tr>
				<td align="right">รหัสแผนก:</td>
				<td>
					<html:text property="departCode" value="${departCode}" styleId="departCode" style="width:90px" />
					<html:errors property="departCode" />
				</td>
				<td ></td>			
 				<td align="right">โครงการ:</td>
 				<td> 					
 					<html:select property="projectCode" value="${projectCode }" styleId="projectCode" style="width:90px">
						<html:optionsCollection name="projectList" label="code" value="code" />
					</html:select> 					
 				</td>
 				<td ></td>				 				 				
 				<td align="right">ส่วนลดบาท:</td>
 				<td>
 					<html:text property="discountAmount" value="${discountAmount}" styleId="discountAmount" onchange="calTaxAmount();" style="text-align:right;width:90px" />
					<html:errors property="discountAmount" />
 				</td>
 				<td ></td>	
 			</tr>
 			<tr>
 				<td align="right">การจัดสรร:</td>
 				<td><html:text property="allocateCode" value="${allocateCode}" styleId="allocateCode" style="width:90px" /> 					
 				</td>
 				<td ></td>
 				<td align="right">วันที่วางบิล:</td>
 				<td>
 					 <html:text property="statementDate" value="${statementDate}" styleId="statementDate" style="width:90px" />
					<html:errors property="statementDate" />
 					
 				</td>
 				<td ></td>
 				<td align="right">ภาษีมูลค่าเพิ่ม:</td>
 				<td>
 					<html:text property="taxAmount" value="${taxAmount}" styleId="taxAmount" onchange="calTaxAmount();" style="text-align:right;width:90px" />
 					<html:errors property="taxAmount"/>
 				</td>
 				<td ></td> 				 				 				
 			</tr>
 			<tr>
 				<td align="right">เครดิต(วัน):</td>
 				<td><html:text property="creditDay" value="${creditDay}" styleId="creditDay" style="width:90px" />
 					<html:errors property="creditDay" />
 				</td>
 				<td ></td>
 				<td align="right">วันที่ครบกำหนด:</td>
 				<td>
 					<html:text property="dueDate" value="${dueDate}" styleId="dueDate" style="width:90px" />
 					<html:errors property="dueDate" />
 				</td>
 				<td ></td> 				
 				<td align="right">มูลค่ารวมภาษี:</td>
 				<td> 					 					
 					<html:text property="totalAmount" value="${totalAmount}" styleId="totalAmount" style="text-align:right;width:90px"  />
					<html:errors property="totalAmount" />
 				</td>
 					
 				<td ></td>	 				 			 				 			 				 				
 			</tr>
 			<tr>
 				<td align="right">กลุ่มเอกสาร:</td>
 				<td>
 					<html:text property="billGroup" value="${billGroup}" styleId="billGroup" style="width:90px" /> 					
 				</td>
 				<td ></td>
 				<td align="right">อัตราภาษีมูลค่าเพิ่ม:</td>
 				<td> 					
 					<html:text property="taxRate" value="${taxRate}" styleId="taxRate" style="text-align:right;width:90px"  />
 					<html:errors property="taxRate"/>
 				</td>
 				<td ></td> 				
 				<td align="right">มูลค่าสินค้ายกเว้นภาษี:</td>
 				<td> 					
 					<html:text property="exceptTaxAmount" value="${exceptTaxAmount}" styleId="exceptTaxAmount"  style="text-align:right;width:90px" />
 					<html:errors property="exceptTaxAmount"/>
 				</td>
 				<td ></td>	 				 			 				 			 				 				
 			</tr> 			 				 	
 			<tr>
 				<td align="right">รหัสสกุลเงิน:</td>
 				<td>
 				
					<html:select property="currencyCode" value="${currencyCode}" style="width:90px" >						
						<html:option value="BAHT">บาท</html:option>						
					</html:select> 				
 				 					
 				</td>
 				<td ></td>	

 				<td align="right">อัตราแลกเปลี่ยน:</td>
 				<td> 					
 					<html:text property="exchangeRate" value="${exchangeRate}" styleId="exchangeRate" style="text-align:right;width:90px"  />
 					<html:errors property="exchangeRate" />
 				</td>
 				<td ></td>	

 				<td align="right">มูลค่าสุทธิ:</td>
 				<td>
 					<html:text property="netAmount" value="${netAmount}" styleId="netAmount" onchange="dataToPayAmount();" style="text-align:right;width:90px"  />
 					<html:errors property="netAmount" />
 					</td>
 				<td ></td>	
 			</tr> 			 				 							
		</table>
		</center>   <br/>  	
		<font size='2'>หมายเหตุ : <font color="red">*</font> คือข้อมูลที่ต้องใส่</font>
     </fieldset></div>
	<div id="userPw01">
	<fieldset>
		<legend><b>บันทึกจ่ายเงิน -วงเงินสดย่อย</b></legend>
		   <center> 
		   <table border="0" >
			<tr>
				<td width="160"align="right">ยอดเงินสดย่อย:</td>
				<td width="140">					
					<html:text property="payAmount" value="${payAmount}" styleId="payAmount" style="text-align:right;width:90px" />
					<html:errors property="payAmount" />
				</td>
				<td width="20">&nbsp;</td>				
				<td width="160"align="right">รหัสวงเงินสดย่อย:</td>
				<td width="160">
					<html:select property="refNo" value="${refNo}" styleId="refNo" style="width:100px">
						<html:option value=""></html:option>						
						<html:option value="ST">ST-Preecha</html:option>						
					</html:select>
					<html:errors property="refNo"/>								
				</td>
				<td width="20">&nbsp;</td>					
			</tr>

		</table>
		</center>
     </fieldset></div>      	
 	<table border="0" align="center" width="100%">
 		<tr>
 			<td align="center" ><html:submit value="บันทึกการซื้อ " onclick="return confirm('ยืนยัน ?');" /></td>
 			<td align="center" ></td>
			<td align="center" ><html:button  value="กลับไปรายการเมนู" property="show1" 
 			onclick="gotoMenu()"  /></td> 			
 		</tr>
 	</table>    	    	    	
    </html:form><br>
    </div>

<script type="text/javascript">

	for(var i=1; i<=15; i++) {
		
		var item = document.getElementById("itemCode"+i).value;
		//alert(item+'=>'+i);
		if(item=="") {
			idArray[i] = 0;
		}else {
			idArray[i] = 1;
		}		
	}
	
function refreshForm() {
	var lAddr = document.getElementById("localAddr").value;
	var lPort = document.getElementById("localPort").value;
	var ctPath = document.getElementById("contextPath").value;
	var docNo = document.getElementById("docNo").value
	window.location = "http://"+lAddr+":"+lPort+ctPath+"/StkReq.do?mode=edit&docNo="+docNo;	
}
function gotoMenu() {
	var lAddr = document.getElementById("localAddr").value;
	var lPort = document.getElementById("localPort").value;
	var ctPath = document.getElementById("contextPath").value;
	window.location = "http://"+lAddr+":"+lPort+ctPath+"/Login.do?mode=check";
}
</script>
<input type="hidden" value="<%=request.getLocalAddr() %>" id="localAddr">
<input type="hidden" value="<%=request.getLocalPort() %>" id="localPort">
<input type="hidden" value="<%=request.getContextPath() %>" id="contextPath">

<!-- 
<a href="#" class="add"><img src="add.png" width="24" height="24" alt="add" title="add input" /></a> 
<a href="#" class="reset"><img src="reset.png" width="24" height="24" alt="reset" /></a>
 -->
 
 </div>
 </div>
 
</body>
</html:html>